import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()  # 创建diver对象：启动浏览器
# # input()
# driver.get("https://www.baidu.com")  # 调用diver对象的方法。注意：路径一定要写对，要有https
# driver.get("https://www.qq.com")
# driver.back()  # 后退到上一个网页
# driver.forward()  # 前进到下一个网页
# driver.refresh()  # 刷新页面
# input()
# print("网页的标题：", driver.title)
# print("网页的网址：", driver.current_url)
# print("网页的内容（HTML）：", driver.page_source)  # 输出形式为字符串，可以进行断言
# cookies = driver.get_cookies()  # 获取全部cookies
# for cookie in cookies:
#     print(cookie)

# # 截取网页的图片，有以下三种方式
# b64_png = driver.get_screenshot_as_base64()  # 字符串：base64
# bin_png = driver.get_screenshot_as_png()  # 二进制字节流
# driver.get_screenshot_as_file("page.png")  # 这个函数的返回值为True，因此直接在括号中写上保存文件的名字即可，会生成这个截图文件

# driver.maximize_window()  # 最大化
# driver.minimize_window()  # 最小化
# driver.fullscreen_window()  # 全屏
# driver.set_window_size(600, 900)  # 指定大小

# driver.get("https://www.baidu.com")  # 调用diver对象的方法。注意：路径一定要写对，要有https
#
# # 输入
# el_a = driver.find_element("xpath", '//*[@id="kw"]')  # 如果XPath正确，可以得到一个指定的对象，如果错误，就报错
# el_b = driver.find_element("xpath", '//*[@id="su"]')
# el_a_list = driver.find_elements("xpath", '//*[@id="kw"]')  # 如果xpath正确，可以得到一个列表，如果错误，就输出一个空列表
# el_b_list = driver.find_elements("xpath", '//*[@id="su"]')
# print(el_a)
# print(el_b)
# print(el_a_list)
# print(el_b_list)
#
# el_a.send_keys("abc")  # 模拟按键
# # el_a.send_keys(Keys.CONTROL+"a")
# el_a.send_keys(Keys.CONTROL + "c")
# el_a.send_keys(Keys.CONTROL + "v")
#
# # 清除
# el_a.clear()  # 清除原有内容
# el_a.send_keys("123")
#
# # 点击
# el_a.click()
# el_b.click()

# driver.get("https://www.baidu.com")  # 调用diver对象的方法。注意：路径一定要写对，要有https
# 文件上传
# 只有包含<input type="file" class="upload-pic" value="上传图片">的按键才能进行文件上传
# el_btn = driver.find_element("xpath", '//*[@id="form"]/span[1]/span[1]')
# el_btn.click()
# time.sleep(1)
# el_a = driver.find_element("xpath", '//*[@id="form"]/div/div[2]/div[2]/input')
# el_a.send_keys(r"E:\PythonText\web_test_0929\page.png")

# driver.get("https://www.baidu.com")  # 调用diver对象的方法。注意：路径一定要写对，要有https
# 获取信息
# el_btn = driver.find_element("xpath", '//*[@id="form"]/span[1]/span[1]')
# el_btn.click()
# time.sleep(1)
# el_a = driver.find_element("xpath", '//*[@id="form"]/div/div[2]/div[2]/input')
# print("文本",el_a.text)
# print("属性",el_a.get_attribute("value"))
# print("标签",el_a.tag_name)
# print("截图",el_a.screenshot("el.png"))

# el_a = driver.find_element("xpath", '//*[@id="s-top-left"]/a[1]')
# print("文本", el_a.text)  # 新闻
# print("属性", el_a.get_attribute("href"))  # http://news.baidu.com/
# print("标签", el_a.tag_name)  # a
# print("截图", el_a.screenshot("el.png"))  # True


# driver.get("https://www.baidu.com")  # 调用diver对象的方法。注意：路径一定要写对，要有https
# 根据元素的不同属性，进行定位
# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
# el_a = driver.find_element(By.ID, 'kw') #ID属性具有唯一性
# print(el_a)
# el_a.screenshot("el.png")

# el_a = driver.find_element(By.TAG_NAME, "input")  # TAG_NAME属性一般都会重复
# print(el_a)
# el_a.screenshot("el.png")  # 报错

# el_a = driver.find_element(By.NAME, "wb")  # NAME属性也有重复的风险
# print(el_a)
# el_a.screenshot("el.png")  # 报错

# el_a = driver.find_element(By.CLASS_NAME, "s_ipt")  # CLASS属性也有重复的风险
# print(el_a)
# el_a.screenshot("el.png")

# 使用标签（超链接）：<a href="http://news.baidu.com" target="_blank" class="mnav c-font-normal c-color-t">新闻</a>
# el_a = driver.find_element(By.LINK_TEXT, "新闻")  # 精确定位
# print(el_a.tag_name, el_a.text)
#
# el_a = driver.find_element(By.PARTIAL_LINK_TEXT, "新")  # 模糊匹配
# print(el_a.tag_name, el_a.text)
#
# el_a = driver.find_element(By.CSS_SELECTOR,'#s-top-left > a:nth-child(1)')
# print(el_a.tag_name, el_a.text)
#
# el_a = driver.find_element(By.XPATH,'//*[@id="s-top-left"]/a[1]')
# print(el_a.tag_name, el_a.text)

# driver.get("http://101.34.221.219:8010/")
# # <p class="price am-text-truncate">￥2499.00</p>
# el_a =driver.find_elements(By.XPATH,'//p[@class="price am-text-truncate"]')
# print(el_a[1].text)
# el_a = driver.find_element(By.XPATH,'//p[@class="price am-text-truncate" and text()="￥2499.00"]')
# print(el_a.text)
#
# el_a = driver.find_element(By.XPATH,'//*[@class="price am-text-truncate" and contains(text(),"￥2499.00")]')
# print(el_a.text)
#
# el_a = driver.find_element(By.XPATH,'//*[@class="price am-text-truncate" and starts-with(text(),"￥2499.00")]')
# print(el_a.text)
#
# input()  # 保留浏览器界面打开
# driver.quit()  # 调用diver对象的方法
